package org.jb.coinche.client.gin;

import org.jb.coinche.client.gui.chat.ChatService;
import org.jb.coinche.client.gui.chat.ChatServiceAsync;
import org.jb.coinche.client.guistatemachine.StateService;
import org.jb.coinche.client.guistatemachine.StateServiceAsync;
import org.jb.coinche.client.notify.NotificationService;
import org.jb.coinche.client.notify.NotificationServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.inject.client.AbstractGinModule;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.inject.Provides;

/**
 * 
 * @author jeremie
 *
 */
public class RoomModule extends AbstractGinModule {

	/*
	 * (non-Javadoc)
	 * @see com.google.gwt.inject.client.AbstractGinModule#configure()
	 */
	@Override
	protected void configure() {}

	private static String roomName = null;
	
	/*
	 * 
	 */
	@Provides @RoomName
	public String getRoomName() {
		if (roomName == null) {
			roomName = Window.Location.getParameter("room");
			if (roomName == null)
				roomName = "Defaut";
		}
		
		return roomName;
	}
	
	/*
	 * On rend les services async dépendant de la salle
	 */
	
	/*
	 * Service de notification
	 */
	@Provides
	public NotificationServiceAsync getNotificationServiceAsync( @RoomName String room ) {
		
		NotificationServiceAsync notificationService = GWT.create(NotificationService.class);
		((ServiceDefTarget)notificationService).setServiceEntryPoint( GWT.getModuleBaseURL() + "rpc?room=" + room );
		
		return notificationService;
	}
	
	/*
	 * Service d'automate
	 */
	@Provides
	public StateServiceAsync getStateServiceAsync( @RoomName String room ) {
		
		StateServiceAsync stateService = GWT.create(StateService.class);
		((ServiceDefTarget)stateService).setServiceEntryPoint( GWT.getModuleBaseURL() + "rpc?room=" + room );
		
		return stateService;
	}
	
	/*
	 * Service de chat
	 */
	@Provides
	public ChatServiceAsync getChatServiceAsync( @RoomName String room ) {
		
		ChatServiceAsync chatService = GWT.create(ChatService.class);
		((ServiceDefTarget)chatService).setServiceEntryPoint( GWT.getModuleBaseURL() + "rpc?room=" + room );
		
		return chatService;
	}
}
